package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.LangUtils;
import cn.e4j.modular.common.service.SysAuthorityService;
import cn.e4j.modular.crm.entity.GwbgT;
import cn.e4j.modular.crm.entity.GwbglT;
import cn.e4j.modular.crm.mapper.GwbgTMapper;
import cn.e4j.modular.crm.service.GwbgTService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  商机主题 服务实现类
 * </p>
 *
 * @author mengyb
 * @since 2019-04-12
 */
@Service
public class GwbgTServiceImpl extends ServiceImpl<GwbgTMapper, GwbgT> implements GwbgTService {
    @Autowired
    private SysAuthorityService sysAuthorityService;

    @Transactional(readOnly = true)
    @Override
    public List<Map<String, Object>> getList(Dto dto, Page<Object> page) {
        ShiroUser shiroUser = ShiroKit.getUser();
        dto.put("ent", shiroUser.getDBC());
        dto.put("lang", LangUtils.getLang(shiroUser.getLANG()));

//        客户权限
        dto.put("CUSTOMERS", sysAuthorityService.getCustomersAuthority(dto));

//        部门权限
        dto.put("ORGS", sysAuthorityService.getOrgsAuthority(dto));

//        业务员权限
        Dto sys004= sysAuthorityService.getSys004(sysAuthorityService.getOrgsAuthority(dto));
        dto.put("ywy",sys004.get("ywy"));


        return this.baseMapper.getList(dto, page);
    }
    @Transactional(readOnly = true)
    @Override
    public List<Map<String, Object>> getGwakPageList(Dto dto, Page<Object> page) {
        ShiroUser shiroUser = ShiroKit.getUser();
        dto.put("ent", shiroUser.getDBC());
        dto.put("lang", LangUtils.getLang(shiroUser.getLANG()));

//        客户权限
        dto.put("CUSTOMERS", sysAuthorityService.getCustomersAuthority(dto));

        return this.baseMapper.getGwakList(dto, page);
    }

    @Transactional(readOnly = true)
    @Override
    public List<Map<String, Object>> getGwakList(Dto dto) {
        ShiroUser shiroUser = ShiroKit.getUser();
        dto.put("ent", shiroUser.getDBC());
        dto.put("lang", LangUtils.getLang(shiroUser.getLANG()));

//        客户权限
        dto.put("CUSTOMERS", sysAuthorityService.getCustomersAuthority(dto));

        return this.baseMapper.getGwakList(dto);
    }
    @Transactional(readOnly = true)
    @Override
    public List<Map<String, Object>> getUserList(Dto dto) {
        return this.baseMapper.getUserList(dto);
    }

    @Transactional(readOnly = true)
    @Override
    public Dto getGwbg005() {
        ShiroUser shiroUser = ShiroKit.getUser();
        Dto outDto = Dtos.newDto();
        String gwbg001 = DateUtils.formatDate(new Date(), "yyyyMMddHHmmssSSS");

        outDto.put("GWBG001", gwbg001);
        outDto.put("GWBG005", shiroUser.getID());
        outDto.put("GWBG005_NAME", shiroUser.getNAME());
        return outDto;
    }
    @Transactional
    @Override
    public void add(GwbgT gwbgT, GwbglT gwbglT) {

        ShiroUser shiroUser = ShiroKit.getUser();

        gwbgT.setGWBGENT(shiroUser.getDBC());
        gwbgT.setGWBGUSER(shiroUser.getACCOUNT());
        gwbgT.setGWBGGRUP(shiroUser.getORG_ID());
        gwbgT.setGWBGORIU(shiroUser.getACCOUNT());
        gwbgT.setGWBGORIG(shiroUser.getORG_ID());
        gwbgT.setGWBGDATE(LocalDate.now());
        gwbgT.setGWBGTIME(DateUtils.formatDate(new Date(), "HH:mm:ss"));

        gwbglT.setGWBGLENT(shiroUser.getDBC());
        gwbglT.setGWBGL001(gwbgT.getGWBG001());
        gwbglT.setGWBGL002(LangUtils.getLang(shiroUser.getLANG()));

        gwbgT.insert();
        gwbglT.insert();
    }
    @Transactional
    @Override
    public void edit(GwbgT gwbgT, GwbglT gwbglT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        gwbgT.setGWBGMODU(shiroUser.getACCOUNT());

        UpdateWrapper<GwbgT> gwbgTUpdateWrapper = new UpdateWrapper<>();
        gwbgTUpdateWrapper.eq("GWBGENT", gwbgT.getGWBGENT());
        gwbgTUpdateWrapper.eq("GWBG001", gwbgT.getGWBG001());

        UpdateWrapper<GwbglT> gwbglTUpdateWrapper = new UpdateWrapper<>();
        gwbglTUpdateWrapper.eq("GWBGLENT", gwbgT.getGWBGENT());
        gwbglTUpdateWrapper.eq("GWBGL001", gwbgT.getGWBG001());

        gwbgT.update(gwbgTUpdateWrapper);
        gwbglT.update(gwbglTUpdateWrapper);
    }
    @Transactional
    @Override
    public void delRow(GwbgT gwbgT, GwbglT gwbglT) {
        UpdateWrapper<GwbgT> gwbgTUpdateWrapper = new UpdateWrapper<>();
        gwbgTUpdateWrapper.eq("GWBGENT", gwbgT.getGWBGENT());
        gwbgTUpdateWrapper.eq("GWBG001", gwbgT.getGWBG001());

        UpdateWrapper<GwbglT> gwbglTUpdateWrapper = new UpdateWrapper<>();
        gwbglTUpdateWrapper.eq("GWBGLENT", gwbgT.getGWBGENT());
        gwbglTUpdateWrapper.eq("GWBGL001", gwbgT.getGWBG001());

        gwbgT.delete(gwbgTUpdateWrapper);
        gwbglT.delete(gwbglTUpdateWrapper);
    }
    @Transactional
    @Override
    public void delRows(List<GwbgT> gwbgTList) {
        for (GwbgT gwbgT : gwbgTList
        ) {
            UpdateWrapper<GwbgT> gwbgTUpdateWrapper = new UpdateWrapper<>();
            gwbgTUpdateWrapper.eq("GWBGENT", gwbgT.getGWBGENT());
            gwbgTUpdateWrapper.eq("GWBG001", gwbgT.getGWBG001());

            GwbglT gwbglT = new GwbglT();
            UpdateWrapper<GwbglT> gwbglTUpdateWrapper = new UpdateWrapper<>();
            gwbglTUpdateWrapper.eq("GWBGLENT", gwbgT.getGWBGENT());
            gwbglTUpdateWrapper.eq("GWBGL001", gwbgT.getGWBG001());

            gwbgT.delete(gwbgTUpdateWrapper);
            gwbglT.delete(gwbglTUpdateWrapper);
        }
    }
    @Transactional(readOnly = true)
    @Override
    public Boolean existedTheParams(GwbgT gwbgT) {
        QueryWrapper<GwbgT> gwbgTQueryWrapper = new QueryWrapper<>();
        gwbgTQueryWrapper.eq("GWBGENT", gwbgT.getGWBGENT());
        gwbgTQueryWrapper.eq("GWBG001", gwbgT.getGWBG001());
        return this.baseMapper.selectList(gwbgTQueryWrapper).size() > 0 ? true : false;
    }
    @Transactional
    @Override
    public void updateGwbg005(List<GwbgT> gwbgTList, String GWBG005) {
        for (GwbgT gwbgT : gwbgTList
        ) {
            UpdateWrapper<GwbgT> gwbgTUpdateWrapper = new UpdateWrapper<>();
            gwbgTUpdateWrapper.eq("GWBGENT", gwbgT.getGWBGENT());
            gwbgTUpdateWrapper.eq("GWBG001", gwbgT.getGWBG001());

            gwbgT.setGWBG005(GWBG005);

            gwbgT.update(gwbgTUpdateWrapper);

        }
    }
}
